package com.aysafety.eventmodel.corn;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.service.soundEmission.SoundEmissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: iotDataPlatform
 * @description: 声发射
 * @author: ChengQi
 * @create: 2019-02-22 11:53
 **/
//@Component
public class SoundEmissionTask {

    @Autowired SoundEmissionService soundEmissionService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void soundEmissionWarning() {
        logger.info("正在计算声发射...");
        String currentTime = DateUtils.getTime();
        try {
            soundEmissionService.soundEmissionWarning(currentTime);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成声发射计算...");
    }

//    @Scheduled(cron = "30 35 10 28 * ?")
    public void soundEmissionWarningHistory() {
        logger.info("正在计算能量平均值...");
        try {
            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 开始时间必须小于结束时间
            Date beginDate = dateFormat1.parse("2019-02-26 21:19:00");
            Date endDate = dateFormat1.parse("2019-02-28 10:30:00");
            Date date = beginDate;
            while (!date.equals(endDate)) {
                String currentTime = DateUtils.getTime(date);
                soundEmissionService.soundEmissionWarning(currentTime);
                c.setTime(date);
                c.add(Calendar.MINUTE, 1);
                date = c.getTime();
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成能量平均值计算...");
    }

}
